/*
 * Copyright (c) 2025 CAX Conversion Project
 */
package com.comac.ins.bomt.domain;

import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.annotation.TableName;
import com.comac.ins.bomt.common.enums.BomDetailTypeEnum;
import com.comac.ins.common.core.exception.BizException;
import com.comac.ins.common.core.utils.BizAssertUtil;
import com.comac.ins.common.json.utils.JsonUtils;
import com.comac.ins.common.tenant.core.TenantEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;

/**
 * Bom信息
 *
 * @author zhanghongyi
 * @date 2024 /8/2 16:10
 * @description BomInfo
 */
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("bomt_bom_info")
public class BomInfo extends TenantEntity {
    /**
     * Id
     */
    private Long id;
    /**
     * 名称
     */
    private String name;
    /**
     * 机型
     */
    private String aircraftModel;
    /**
     * 跟踪Id（用于标记产出数据的业务流程Id）
     */
    private String track;
    /**
     * 子跟踪Id（用于标记产出的业务流程步骤）
     */
    private String subTrack;
    /**
     * ATA 100
     */
    private String ata;
    /**
     * 主分组类型
     */
    private String groupType;
    /**
     * 子分组类型
     */
    private String subGroupType;
    /**
     * 状态
     */
    private String status;
    /**
     * 业务步骤
     */
    private String bizStep;

    /**
     * 标准装机清单摘要
     */
    private String standardBomSummary;
    /**
     * 标准装机清单文件Id
     */
    private Long standardBomFileId;
    /**
     * 强度数模清单摘要
     */
    private String strengthBomSummary;
    /**
     * 强度数模清单文件Id
     */
    private Long strengthBomFileId;
    /**
     * 模型状态清单摘要
     */
    private String modelStatusBomSummary;
    /**
     * 模型状态清单文件Id
     */
    private Long modelStatusBomFileId;
    /**
     * 强度数模清单Vs标准装机清单摘要
     */
    private String strengthVsStandardSummary;
    /**
     * 强度数模清单Vs标准装机清单文件Id
     */
    private Long strengthVsStandardFileId;
    /**
     * 模型状态清单Vs强度数模清单摘要
     */
    private String statusVsStrengthSummary;
    /**
     * 模型状态清单Vs强度数模清单文件Id
     */
    private Long statusVsStrengthFileId;
    /**
     * 附加信息
     *
     * @see BomExtraInfo
     */
    private String extraInfo;

    public void setErrorMsg(String errMsg) {
        BomExtraInfo extraInfoObj;
        if (StringUtils.isBlank(extraInfo)) {
            extraInfoObj = new BomExtraInfo();
        } else {
            extraInfoObj = JsonUtils.parseObject(extraInfo, BomExtraInfo.class);
            BizAssertUtil.notNull(extraInfoObj, "parse extra info fail");
        }
        extraInfoObj.setErrMsg(errMsg);
        extraInfo = JsonUtils.toJsonString(extraInfoObj);
    }

    /**
     * 替换文件Id
     *
     * @param detailType 明细类型 @see BomDetailTypeEnum
     * @param fileId     新的文件Id
     */
    public void replaceFileId(BomDetailTypeEnum detailType, Long fileId) {
        BizAssertUtil.notNull(fileId, "fileId is null");
        BizAssertUtil.notNull(detailType, "detailType is null");
        BomExtraInfo extraInfoObj;
        if (StringUtils.isBlank(extraInfo)) {
            extraInfoObj = new BomExtraInfo();
        } else {
            extraInfoObj = JsonUtils.parseObject(extraInfo, BomExtraInfo.class);
            BizAssertUtil.notNull(extraInfoObj, "parse extra info fail");
        }
        boolean modifyHis = false;
        switch (detailType) {
            case SBOM:
                if (standardBomFileId != null) {
                    if (CollectionUtils.isEmpty(extraInfoObj.getStandardBomFileHistory())) {
                        extraInfoObj.setStandardBomFileHistory(new ArrayList<>());
                    }
                    extraInfoObj.getStandardBomFileHistory().add(standardBomFileId);
                    modifyHis = true;
                }
                standardBomFileId = fileId;
                break;
            case QBOM:
                if (strengthBomFileId != null) {
                    if (CollectionUtils.isEmpty(extraInfoObj.getStrengthBomFileHistory())) {
                        extraInfoObj.setStrengthBomFileHistory(new ArrayList<>());
                    }
                    extraInfoObj.getStrengthBomFileHistory().add(strengthBomFileId);
                    modifyHis = true;
                }
                strengthBomFileId = fileId;
                break;
            case QBOM_SBOM_CHANGE:
                if (strengthVsStandardFileId != null) {
                    if (CollectionUtils.isEmpty(extraInfoObj.getStrengthVsStandardFileHistory())) {
                        extraInfoObj.setStrengthVsStandardFileHistory(new ArrayList<>());
                    }
                    extraInfoObj.getStrengthVsStandardFileHistory().add(strengthVsStandardFileId);
                    modifyHis = true;
                }
                strengthVsStandardFileId = fileId;
                break;
            case MSTS_QBOM_CHANGE:
                if (statusVsStrengthFileId != null) {
                    if (CollectionUtils.isEmpty(extraInfoObj.getStatusVsStrengthFileHistory())) {
                        extraInfoObj.setStatusVsStrengthFileHistory(new ArrayList<>());
                    }
                    extraInfoObj.getStatusVsStrengthFileHistory().add(statusVsStrengthFileId);
                    modifyHis = true;
                }
                statusVsStrengthFileId = fileId;
                break;
            default:
                throw new BizException("unsupported detail type");
        }
        if (modifyHis) {
            extraInfo = JsonUtils.toJsonString(extraInfoObj);
        }
    }
}
